/**
 * Created by kangli on 2018/4/23.
 */
var sum;
var counts='';
var pageThis =0;
var pagecount = 10;
var index = 0;
var wheretext = '';

$(function(){
    //得到用户列表
    getuserLists();
    $(".searchNameInput").keypress(function(e) {
        // 回车键事件
        if(e.which == 13) {
            e.preventDefault();
            getuserLists();
        }
    });

    $('#addUser').on('shown.bs.modal', function () {
        getSchoolList();
    });

})
function getuserLists(){
    wheretext=$.trim($(".searchNameInput").val());
    var typeArray = ['SCHOOLADMIN','OTHER'];
    var  data={page: index ,size: pagecount,teacherName:wheretext,type:typeArray};
    AjaxSubmit("GET",juge(data), basePath + "/super/common/user/searchTeacher",searchTeacher_fun);
}
function searchTeacher_fun(res){
    if(res.status=='SUCCESS'){
        var userLists = res.obj.content;
        sum = res.obj.totalElements;
        counts = sum / pagecount;
        if (userLists.length != 0) {
            $("#userPage").css("display","block");
            if (sum % pagecount == 0) {
                counts = parseInt(sum / pagecount);
            }
            else {
                counts = parseInt(sum / pagecount) + 1;
            }
            $("#userPage").paging({
                pageNo: pageThis,
                totalPage: counts,
                totalSize: sum,
                callback: function (num) {
                    index = num - 1;
                    getuserLists();
                    pageThis = num;
                }
            });
            $(".userTbody").empty();
            $('.warnDiv').remove();
            $.each(userLists, function (i, userList) {
                var type = '';
                var statustr='';
                var username = userList.username || '';
                var teacherName = userList.teacherName || '';
                var schoolName = userList.schoolName || '';
                if(userList.type=='SCHOOLADMIN'){
                    type='校管理员'
                }else if(userList.type=='OTHER'){
                    type='其他'
                }
                var html = '<tr> ' +
                    '<td class="texC">'+schoolName+'</td> ' +
                    '<td class="texC">'+username+'</td> ' +
                    '<td class="texC">'+teacherName+'</td> ' +
                    '<td class="texC"> '+type+'</td> ' +
                    '<td class="texC"><span class=" cursorP  colorBlue"  data-id="'+userList.id+'"  onclick="deleteUser(this)">删除</span></td> ' +
                    '</tr>';
                $(".userTbody").append(html)
            })

        }
        else{
            $("#userPage").css("display","none");
            $(".userTbody").empty();
            $('.warnDiv').remove();
            var html = $('<div class="text-center warnDiv "  style="width: 100%;margin: 73px 0px;font-size: 14px" >无信息!</div>');
            $(".userTable").after(html)
        }
    }
}

//得到所有的学校
function getSchoolList(){
    var data = '{"page":"' + index + '","size":"' + 10000 + '"}';
    AjaxSubmit("GET",JSON.parse(data),basePath + "/super/common/school/getSchool",searchSchool_fun);
}

function searchSchool_fun(res){
    if(res.obj.content.length!=0){
        var schoolLists = res.obj.content;
        $(".schoolSelect").empty();
        $.each(schoolLists,function(i,schoolList){
            $(".schoolSelect").append('<option>'+schoolList.schoolName+'</option>');
        })
    }else{
        $(".schoolSelect").append('<option>请选择</option>');
    }
}

/*新增用户*/
function addUser(btn){
    var schoolid =   $(".schoolSelect").children("option:selected").text();
    var username = $(".um-username").val();
    var name = $(".um-name").val();
    var pass = $(".um-pass").val();
    var type=$(".accountType").children("option:selected").text();
    var truetype='';
    if(type=='校管理员'){
        truetype='SCHOOLADMIN'
    }else if(type=='其他'){
        truetype='OTHER'
    }
    var question =(username!=""&&username!=undefined&& username!= null)?(name!=""&&name!=undefined&& name!= null)? pass!=""&&pass!=undefined&& pass!= null?
        "UMtrue":confirm("密码不为空") :confirm("姓名不为空"):confirm("用户名不为空");


    if(question=='UMtrue'){
        var data = '{"schoolName":"' + schoolid + '","username":"' + username + '","password":"' + pass + '","type":"' + truetype + '","teacherName":"' + name + '"}';
        AjaxSubmit("POST",JSON.parse(data),basePath + "/super/common/user/addUser",addUser_fun);
        $("#addUser").modal("hide");
    }
}
function addUser_fun(res){
    if(res.status=='SUCCESS'){
        index=$("#userPage").find(".active").find("a").text()-1;
        getuserLists();
    }
}

/*删除用户*/
function deleteUser(btn){
    var id = $(btn).attr("data-id");
    var msg = "您真的确定要删除吗？\n\n请确认！";
    if (confirm(msg)==true){
        var  data={id: id};
        AjaxSubmit("POST",data, basePath + "/super/common/user/deleteUser",deleteUser_fun);
        function deleteUser_fun(res){
            if(res.status=='SUCCESS'){
                index=0;
                getuserLists();
            }
        }
    }else{
        return false;
    }
}

//判断字符不空就存
function juge(obj){
    var result={};
    for(i in obj){
        if(obj[i]==null||obj[i]==""){
            continue;
        }
        var value=obj[i];
        eval("result."+i+"='"+value+"'");
    }
    return result;
}